#!/bin/sh

mode=$1
scripts=/usr/local/cutedge/openvpn/scripts

cat $scripts/mail_threats.txt | sed '/^#/d' | sort | uniq > $scripts/mail_threats.tmp

todayDate=`date '+%F %T'`
echo "### last checked on - $todayDate ###" >> $scripts/mail_threats.tmp

(cat /var/log/postfix.log ; (test -f /var/log/postfix.log.0) && cat /var/log/postfix.log.0) | grep 'SASL LOGIN authentication failed:' | awk -F '\\: warning\\: .*\\[|\\]\\: SASL LOGIN' '{print $2}' | sort | uniq > $scripts/mail_threats_log.tmp

for i in `cat $scripts/mail_threats_log.tmp`
do
    (grep -Fxq "$i" $scripts/mail_threats.tmp) || echo $i >> $scripts/mail_threats.tmp
done

rm $scripts/mail_threats_log.tmp

if (test $mode) then
    (cat $scripts/mail_threats.tmp | sed '/^#/d' | sort | uniq ; echo "### last checked on - $todayDate ###") > $scripts/mail_threats.txt
    pfctl -a com.cutedgesystems.fw -f $scripts/fwrules
else
    cat $scripts/mail_threats.tmp
fi

rm $scripts/mail_threats.tmp
